﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using IntelligentStorWpfApp.Controller;
using System.IO;
using System.Collections.Generic;
using IntelligentStorWpfApp.Model;
using System.Linq;

namespace IntelligentStorWpfApp.View
{
    /// <summary>
    /// productsData.xaml 的交互逻辑
    /// </summary>
    public partial class ProductsData : Window
    {
        public ProductsData()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
           // this.Topmost = true;
        }
        private ProductController prodController;
        private List<ProductInfo> listPrds;
        private void ProductsData_Loaded(object sender, RoutedEventArgs e)
        {
            prodController = new ProductController();
            listPrds = new List<ProductInfo>();
            if (App.user.FaceId!=null)
            {
                BitmapImage bmp = null;

                try
                {
                    bmp = new BitmapImage();
                    bmp.BeginInit();
                    bmp.StreamSource = new MemoryStream(App.user.FaceId);
                    bmp.EndInit();
                }
                catch
                {
                    bmp = null;
                }
                if(bmp!=null)
                    userImage.Source = bmp;
                else
                    userImage.Source = new BitmapImage(new Uri(@"/Image/defaultpic.jpg", UriKind.RelativeOrAbsolute));

            }
            else
            {
                userImage.Source = new BitmapImage(new Uri(@"/Image/defaultpic.jpg", UriKind.RelativeOrAbsolute));
            }
            userImage.Stretch = Stretch.Uniform;
            lblusername.Text = "用户名: "+App.user.UserName;
            listPrds = prodController.getProductsByUserName(App.user.UserName);
            bindingData(App.Num, 1);
        }

        private void dgProducts_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }

        //number表示每个页面显示的记录数    currentSize表示当前显示页数
        private void bindingData(int number, int currentSize)
        {
            List<ProductInfo> infoList = new List<ProductInfo>();
            infoList = listPrds;      //获取数据源
            int count = infoList.Count;          //获取记录总数
            int pageSize = 0;            //pageSize表示总页数
            if (count % number == 0)
            {
                pageSize = count / number;
            }
            else
            {
                pageSize = count / number + 1;
            }
            tbkTotal.Text = pageSize.ToString();

            tbkCurrentsize.Text = currentSize.ToString();
            infoList = infoList.Take(number * currentSize).Skip(number * (currentSize - 1)).ToList();   //刷选第currentSize页要显示的记录集
            dgProducts.ItemsSource = null;
            dgProducts.ItemsSource = infoList;        //重新绑定dataGrid1
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            int currentsize = int.Parse(tbkCurrentsize.Text); //获取当前页数
            if (currentsize > 1)
            {
                bindingData(App.Num, currentsize - 1);   //调用分页方法
            }
        }
        private void btnGo_Click(object sender, RoutedEventArgs e)
        {
            int pageNum = int.Parse(tbxPageNum.Text);
            int total = int.Parse(tbkTotal.Text); //总页数
            if (pageNum >= 1 && pageNum <= total)
            {
                bindingData(App.Num, pageNum);     //调用分页方法
            }
        }
        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            int total = int.Parse(tbkTotal.Text); //总页数
            int currentsize = int.Parse(tbkCurrentsize.Text); //当前页数
            if (currentsize < total)
            {
                bindingData(App.Num, currentsize + 1);   //调用分页方法
            }
        }
    }
}
